package cn.dengl.sky.springboot.common.config.util;

import java.util.Random;

/**
 * @program: springboot
 * @description: 随机数
 * @author: dengl
 * @create: 2018-09-18 13:55
 */
public class RandomNoUtil {
    private static Random random;
    private static long seed;
    static {
        seed = System.currentTimeMillis();
        random = new Random(seed);
    }

    public static String getRandomNo(String maxNo,int index ){
        String snNumber=null;
       String dateStr= DataUtil.getNowDateStringType("YYYYMMdd");
        String name=CommonEnum.getName(index);
        String uid_pfix="";
        if(name!=null){
            if("case".equalsIgnoreCase(name)){
                uid_pfix="NO100"+dateStr;
            }
            if("loss".equalsIgnoreCase(name)){
                uid_pfix="NO101"+dateStr;
            }
        }
        if(!"".equals(uid_pfix)){
            if(maxNo!=null&&!"".equals(maxNo)){
//                String uid_end =maxNo.substring(13,18);
                int endNum=Integer.parseInt(maxNo);
                int resultNum=endNum+1;
                snNumber=uid_pfix+resultNum;
            }else{
                snNumber=uid_pfix+"10001";
            }
        }else{
            snNumber="erro"+dateStr+10000;
        }
        return snNumber;
    }

    /**
     * 指定长度字符，有概率只包含一种（数字、大写字母、小写字母），最低长度为1
     * @param length
     * @return
     */
    public static String getRandomString(int length){
        if(length<=0){
            length=1;
        }
        String str="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();

        StringBuffer sb=new StringBuffer();

        for(int i=0;i<length;i++){

            int number =random.nextInt(62);

            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        System.out.println(getTaskNoDateCharter6());
        //System.out.println(getGeneratePassword(6));
    }

    /**
     * 获取时间加6位随机码（一定包含大小写字母以及数字）
     * @return
     */
    public static  String getTaskNoDateCharter6(){
        String date=DataUtil.getNowDateStringType("yyyyMMddHHmmss");
        String taskNo=date+getGeneratePassword(6);
        return taskNo;
    }

    /**
     * 6位随机码（一定包含大小写字母以及数字）最小长度为3
     * @param len 指定字符长度
     * @return
     */
    public static String getGeneratePassword(int len) {
        if(len<=3){
            len=3;
        }
        Random random=new Random();
        char[] chArr = new char[len];
        chArr[0] = (char)('0' + random.nextInt(10) );
        chArr[1] = (char)('A' + random.nextInt(26));
        chArr[2] = (char)('a' + random.nextInt(26));
        char[] codes = { '0','1','2','3','4','5','6','7','8','9',
                                         'A','B','C','D','E','F','G','H','I','J',
                                       'K','L','M','N','O','P','Q','R','S','T',
                                        'U','V','W','X','Y','Z','a','b','c','d',
                                        'e','f','g','h','i','j','k','l','m','n',
                                       'o','p','q','r','s','t','u','v','w','x',
                                      'y','z'};
               //charArr[3..len-1]随机生成codes中的字符
                for(int i = 3; i < len; i++){
                      chArr[i] = codes[random.nextInt(codes.length)];
                  }

                //将数组chArr随机排序
                for(int i = 0; i < len; i++){
                        int r = i + random.nextInt(len - i);
                        char temp = chArr[i];
                        chArr[i] = chArr[r];
                        chArr[r] = temp;
                  }

                return new String(chArr);
    }
}
